package net.joey.file;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class StreamReaderWriterDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
//			File f = new File("AFile.java");
			File f = new File("PrintSteam(utf-8).cc");
//			File f = new File("PrintSteam.cc");
			InputStreamReader isr = new InputStreamReader(new FileInputStream(f),"gbk");
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("CopyFileStreamReaderWriter.cc"));
			
			int r = -1;
			while((r = isr.read()) != -1){
				System.out.println(Integer.toHexString(r));
				System.out.println((char)(r));
				osw.write(r);
			}
			System.out.println(osw.getEncoding());
//			osw.write(0x6d93);
//			osw.write(0xe15f);
//			osw.write(0x6783);
			
			isr.close();
			osw.close();
			System.out.println("*************");
			int rr = -1;
			FileInputStream fis = new FileInputStream(f);
			while((rr = fis.read()) != -1){
				System.out.println(Integer.toHexString(rr));
			}
			fis.close();
			fis = new FileInputStream(f);
			byte[] bs=new byte[3];   
			fis.read(bs);   
			System.out.println("*************");
			for(byte b:bs){
				System.out.println(Integer.toHexString(b));
			}
			fis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
